package com.mingqijia.gassafety.webserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.db.entity.ConsumerLocation;
import com.mingqijia.gassafety.db.entity.FieldTemplate;
import com.mingqijia.gassafety.db.entity.Organization;
import com.mingqijia.gassafety.db.mapper.ConsumerLocationMapper;
import com.mingqijia.gassafety.db.mapper.FieldTemplateMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.shared.constant.*;
import com.mingqijia.gassafety.shared.dto.LogVO;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.webserver.service.FieldTemplateService;
import com.mingqijia.gassafety.webserver.service.OrganizationService;
import com.mingqijia.gassafety.webserver.utils.LogRecordAspect;
import com.mingqijia.gassafety.webserver.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 页面字段展示模式 服务实现类
 * </p>
 *
 * @author lishiying
 * @since 2022-09-14
 */
@Service
@Slf4j
public class FieldTemplateServiceImpl extends ServiceImpl<FieldTemplateMapper, FieldTemplate> implements FieldTemplateService {

    @Autowired
    OrganizationService organizationService;
    
    @Autowired
    ConsumerLocationMapper consumerLocationMapper;

    @Override
    public void setTemplateType(FieldTemplate template) {
        String spId = ApplicationUtils.getWorkingSpId();
        LambdaQueryWrapper<Organization> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Organization::getIsDeleted, Constants.IS_DELETED_FALSE)
                .and(lqw1-> {
                    lqw1.eq(Organization::getParentId, spId).or(
                            lqw2 ->{
                                lqw2.eq(Organization::getSpId, spId).isNotNull(Organization::getParentId);
                            });
                });
        List<Organization> list = organizationService.list(lqw);
        if (!CollectionUtils.isEmpty(list) ){
            throw new BizException("当前组织在字典中配置了层级关系，模板不可修改！");
        }
        LambdaQueryWrapper<FieldTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FieldTemplate::getSpId, spId).eq(FieldTemplate::getIsDeleted, Constants.IS_DELETED_FALSE);
        FieldTemplate one = this.baseMapper.selectOne(lambdaQueryWrapper);
        FieldTemplate gssFieldTemplate = new FieldTemplate();
        gssFieldTemplate.setTemplateType(template.getTemplateType());
        gssFieldTemplate.setWorkSystemType(template.getWorkSystemType());
        gssFieldTemplate.setFuncAreaAllocat(template.getFuncAreaAllocat());
        gssFieldTemplate.setSkWorkSystemType(template.getSkWorkSystemType());
        gssFieldTemplate.setAlarmHandleType(template.getAlarmHandleType());
        gssFieldTemplate.setBrokenHandleType(template.getBrokenHandleType());
        if(one != null){
            gssFieldTemplate.setId(one.getId());
            this.updateById(gssFieldTemplate);
            //企业级操作日志
            LogVO logVO = new LogVO();
            logVO.setDetail(LogUtil.compareEntity(gssFieldTemplate,one, FieldTemplate.class));
            LogRecordAspect.threadLocal.set(logVO);
        } else {
            gssFieldTemplate.setSpId(spId);
            this.save(gssFieldTemplate);
        }

    }

    @Override
    public FieldTemplate getBySpId() {
        String spId = ApplicationUtils.getWorkingSpId();
        LambdaQueryWrapper<FieldTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FieldTemplate::getSpId, spId).eq(FieldTemplate::getIsDeleted, Constants.IS_DELETED_FALSE);
        FieldTemplate fieldTemplate = this.baseMapper.selectOne(lqw);
        if (fieldTemplate == null){
            FieldTemplate template = new FieldTemplate();
            template.setTemplateType(TemplateTypeEnum.TEMPLATE_TYPE_1.getCode());
            template.setWorkSystemType(WorkSystemEnum.MBP_SYSTEM.getCode());
            template.setFuncAreaAllocat(Constants.FUNC_AREA_ALLOCAT_AUTO);
            template.setSkWorkSystemType(SkWorkSystemEnum.MBP_SYSTEM.getCode());
            template.setAlarmHandleType(AlarmHandleEnum.MBP_SYSTEM.getCode());
            template.setBrokenHandleType(BrokenHandleEnum.MBP_SYSTEM.getCode());
            template.setSpId(spId);
            this.baseMapper.insert(template);
            log.info("getBySpId template insert:{}",template);
            return template;
        }
        return fieldTemplate;
    }

    @Override
    public FieldTemplate getTemplate(String spId) {
        LambdaQueryWrapper<FieldTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FieldTemplate::getSpId, spId).eq(FieldTemplate::getIsDeleted, Constants.IS_DELETED_FALSE);
        FieldTemplate fieldTemplate = this.baseMapper.selectOne(lqw);
        if (fieldTemplate == null) {
            FieldTemplate template = new FieldTemplate();
            template.setTemplateType(TemplateTypeEnum.TEMPLATE_TYPE_1.getCode());
            template.setWorkSystemType(WorkSystemEnum.MBP_SYSTEM.getCode());
            template.setFuncAreaAllocat(Constants.FUNC_AREA_ALLOCAT_AUTO);
            template.setSkWorkSystemType(SkWorkSystemEnum.MBP_SYSTEM.getCode());
            template.setAlarmHandleType(AlarmHandleEnum.MBP_SYSTEM.getCode());
            template.setBrokenHandleType(BrokenHandleEnum.MBP_SYSTEM.getCode());
            template.setSpId(spId);
            log.info("getTemplate template insert:{}",template);
            this.baseMapper.insert(template);
            return template;
        }
        return fieldTemplate;
    }

    @Override
    public FieldTemplate getTemplateBySpId(String spId) {
        LambdaQueryWrapper<FieldTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FieldTemplate::getSpId, spId).eq(FieldTemplate::getIsDeleted, Constants.IS_DELETED_FALSE);
        return this.baseMapper.selectOne(lqw);
    }


    @Override
    public int getBySpId(String spId) {
        LambdaQueryWrapper<FieldTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FieldTemplate::getSpId, spId);
        FieldTemplate fieldTemplate = this.baseMapper.selectOne(lqw);
        if (fieldTemplate == null){
            return TemplateTypeEnum.TEMPLATE_TYPE_1.getCode();
        }
        return fieldTemplate.getTemplateType();
    }

    @Override
    public boolean isHuishengWorkSystemMode(String spId) {
        LambdaQueryWrapper<FieldTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FieldTemplate::getSpId, spId);
        FieldTemplate fieldTemplate = this.baseMapper.selectOne(lqw);
        if (fieldTemplate == null){
            return false;
        }
        return Objects.equals(SkWorkSystemEnum.HUISHENG_SYSTEM.getCode(), fieldTemplate.getSkWorkSystemType());
    }
}
